package com.t.e.leaksdemo;

import android.app.ActivityManager;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.leakcanary.RefWatcher;


public class LeakFrgmt extends Fragment {

    TextView tv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.frgmt_leaks, container, false);
        tv = (TextView) v.findViewById(R.id.tv_leak_frgmt);

        ActivityManager activityManager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(info);
        tv.setText("meminfo 系统剩余内存:" + (info.availMem >> 10) + "k" + "\n");
        tv.append("系统是否处于低内存运行：" + info.lowMemory + "\n");
        tv.append("当系统剩余内存低于" + (info.threshold >> 10) + "k" + "时就看成低内存运行" + "\n");
        tv.append("本Fragment，重复进入本fragment，旋转屏幕可暴漏");

        TestDataModel.getInstance().setRetainedTextView(tv);
        return v;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        RefWatcher refWatcher = LeaksDemoApp.getRefWatcher(getActivity());
        refWatcher.watch(this);
        System.out.println("LeakFrgmt:onDestroy");
    }
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);

        switch (level){
            case TRIM_MEMORY_RUNNING_MODERATE:
                break;
            case TRIM_MEMORY_UI_HIDDEN://这里释放 ui资源
                break;
            default:break;
        }
    }

}
